#!/usr/bin/python3
import pymysql


def conn():
    # 连接database
    c = pymysql.connect(
        host="127.0.0.1",
        port=23306,
        user='test',
        password='test',
        db='dockertest',
        charset='utf8'
    )

    return c


def find_res(val):
    # 打开数据库连接
    db = conn()

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    # 使用 execute()  方法执行 SQL 查询
    # cursor.execute("SELECT VERSION()")

    # SQL 查询语句
    v = val.replace(" ", "")
    v = v.replace("\n", "")
    # print(v)

    sql = "SELECT distinct `type`,`dice`,`result`,`A`,`B`,`C`,`D`,`E` FROM `dockertest`.`t_question` WHERE `dice` LIKE '%" + v + "%' LIMIT 0,5"

    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表

        results = cursor.fetchall()
        i = 1

        for row in results:

            type = row[0]
            title = row[1]
            res = row[2]
            A = row[3]
            B = row[4]
            C = row[5]
            D = row[6]
            E = row[7]

            # 打印结果
            if type == "判断题":
                print("*******************************" + str(i))
                print("title=%s,res=%s" % (title, res))
            else:
                print("*******************************" + str(i))
                print("title=%s,res=%s,A=%s,B=%s,C=%s,D=%s,E=%s" % (title, res, A, B, C, D, E))
            i = i + 1

    except:
        print("Error: unable to fetch data")

    # 使用 fetchone() 方法获取单条数据.
    # data = results
    # print(data)

    # 关闭数据库连接
    db.close()


if __name__ == '__main__':
    find_res("OSI安全框架的")
